<?php

namespace app\controller;
class ProxyController {
    private $ipPlatform = 1;
    private $proxyArea = '';

    const IP_PLATFORM_ZHILIAN = 2;
    const IP_PLATFORM_XIEQU = 4;
    const IP_PLATFORM_XIEQU60 = 5;
    const IP_PLATFORM_QINGGUO_3MINUTE = 8;

    public function getProxy() {
        $ipPlatform = request()->post('ip_platform', 1);
        $area = request()->post('area' , '');
        $this->proxyArea = $area;
        $this->ipPlatform = $ipPlatform;

        try{
            switch ($ipPlatform) {
                case self::IP_PLATFORM_XIEQU:
                    $res = $this->proxyXiequ();
                    break;
                case self::IP_PLATFORM_ZHILIAN:
                    $res = $this->proxyZhilian();
                    break;
                case self::IP_PLATFORM_XIEQU60:
                    $res = $this->proxyXiequ60();
                    break;
                case self::IP_PLATFORM_QINGGUO_3MINUTE:
                    $res = $this->proxyQingguo3Minute();
                    break;
                default:
                    $res = $this->proxyQingguo();
                    break;
            }
            $res = ['code' => 200 , 'data' => $res];
        }catch (\Exception $e){
            $res = ['code' => 404 , 'msg' => $e->getMessage()];
        }
        return json_encode($res , 320);
    }

    private function proxyXiequ() {
        $url = "http://api.xiequ.cn/VAD/GetIp.aspx?act=get&uid=140598&vkey=76EF1833476E08AC974C66AD5ECEEBAB&num=1&time=30&plat=1&re=0&type=2&so=1&ow=1&spl=1&addr={$this->proxyArea}&db=1";
        $response = file_get_contents($url);
        $proxyAddr = trim($response);
        return ['xiequ30', $proxyAddr, 'http://' . $proxyAddr];
    }

    private function proxyXiequ60() {
        $url = "http://api.xiequ.cn/VAD/GetIp.aspx?act=get&uid=140598&vkey=76EF1833476E08AC974C66AD5ECEEBAB&num=1&time=60&plat=1&re=0&type=2&so=1&ow=1&spl=1&addr={$this->proxyArea}&db=1";
        $response = file_get_contents($url);
        $proxyAddr = trim($response);
        return ['xiequ60', $proxyAddr, 'http://' . $proxyAddr];
    }

    private function proxyZhilian() {
        $ipUrl = 'http://www.zhiliandaili.com/api3/getapi2?linePoolIndex=-1&packid=2&qty=1&time=2&port=1&format=txt&ss=1&ipport=1&et=1&dt=4&isp=0&ct=0&uid=48306&usertype=17';
        $content = trim(file_get_contents($ipUrl));
        list($ip , $expireTime) = explode('|' , $content);
        return ['zhilian|' . $expireTime, $ip, 'http://' . $ip];
    }

    private function proxyQingguo($authKey = 'RZ1DSFHG', $password = '0DC9E4B5A87D', $title = 'qingguo') {
        $params = [
            'key' => $authKey,
            'num' => '1',
            'area' => $this->proxyArea,
            'isp' => '',
            'format' => 'txt',
            'seq' => '%5Cr%5Cn',
            'distinct' => 'true',
            'pool' => '2'
        ];
        $query = http_build_query($params);
        $url = "https://share.proxy.qg.net/get?" . $query;
        $response = file_get_contents($url);

        $proxyAddr = trim($response);
        $proxyUrl = "http://{$authKey}:{$password}@{$proxyAddr}";
        if (strpos($proxyAddr, '提取失败') !== false) {
            sleep(1); // Wait for 1 second
            return $this->proxyQingguo($authKey, $password, $title);
        }

        if (strpos($proxyAddr, '{') !== false) {
            $res = json_decode($proxyAddr, true);
            if ($res['code'] === 'REQUEST_LIMIT_EXCEEDED') {
                sleep(1); // Wait for 1 second
                return $this->proxyQingguo($authKey, $password, $title);
            }
        }

        return [$title, $proxyAddr, $proxyUrl];
    }

    private function proxyQingguo3Minute() {
        return $this->proxyQingguo('HQ7BPJ0C', '57E6F82F108F', 'qingguo3分钟');
    }
}